package com.biz.primus.ms.evaluation.convertor;

import com.biz.primus.model.evaluation.vo.DeliveryEvaluationVO;
import com.biz.primus.model.evaluation.vo.OrderEvaluationVO;
import com.biz.primus.model.evaluation.vo.ProductEvaluationVO;
import com.biz.primus.model.order.exception.utils.AssertRequestParameter;
import com.biz.primus.ms.evaluation.dao.po.DeliveryEvaluation;
import com.biz.primus.ms.evaluation.dao.po.OrderEvaluation;
import com.biz.primus.ms.evaluation.dao.po.ProductEvaluation;
import org.codelogger.utils.BeanUtils;
import org.codelogger.utils.StringUtils;

import java.util.stream.Collectors;

/**
 * 评价VO与PO转换器
 *
 * @author Tiger Feng
 * @date 2017年11月06日
 */
public class EvaluationConvertor {
    /**
     * 物流评价VO转PO
     *
     * @param vo
     * @param orderCode
     * @param depotCode
     * @return
     */
    public static DeliveryEvaluation vo2PO(DeliveryEvaluationVO vo, String orderCode, String depotCode) {
        if (vo == null) {
            return null;
        }
        AssertRequestParameter.notNull(orderCode, "订单编号不能为空");
        AssertRequestParameter.notNull(depotCode, "门店编号不能为空");
        DeliveryEvaluation po = new DeliveryEvaluation();
        BeanUtils.copyProperties(vo, po);
        po.setOrderCode(orderCode);
        po.setDepotCode(depotCode);
        return po;
    }

    /**
     * 商品评价VO转PO
     *
     * @param vo
     * @param orderCode
     * @param depotCode
     * @return
     */
    public static ProductEvaluation vo2PO(ProductEvaluationVO vo, String orderCode, String depotCode) {
        if (vo == null) {
            return null;
        }
        AssertRequestParameter.notNull(orderCode, "订单编号不能为空");
        AssertRequestParameter.notNull(depotCode, "门店编号不能为空");
        ProductEvaluation po = new ProductEvaluation();
        BeanUtils.copyProperties(vo, po);
        po.setOrderCode(orderCode);
        po.setDepotCode(depotCode);
        return po;
    }

    /**
     * 订单评价VO转PO
     *
     * @param vo
     * @return
     */
    public static OrderEvaluation vo2PO(OrderEvaluationVO vo) {
        if (vo == null) {
            return null;
        }
        OrderEvaluation po = new OrderEvaluation();
        BeanUtils.copyProperties(vo, po);
        if (vo.getDeliveryEvaluation() != null) {
            po.setDeliveryEvaluation(vo2PO(vo.getDeliveryEvaluation(), vo.getOrderCode(), vo.getDepotCode()));
        }
        if (vo.getProductEvaluations() != null && !vo.getProductEvaluations().isEmpty()) {
            po.setProductEvaluations(
                    vo.getProductEvaluations()
                            .stream()
                            .map(productEvaluationVO -> vo2PO(productEvaluationVO, vo.getOrderCode(), vo.getDepotCode()))
                            .collect(Collectors.toList()));
        }
        return po;
    }

    /**
     * 订单评价PO转VO
     * @param po
     * @return
     */
    public static OrderEvaluationVO po2VO(OrderEvaluation po) {
        if (po == null) {
            return null;
        }
        OrderEvaluationVO vo = new OrderEvaluationVO();
        BeanUtils.copyProperties(po, vo);
        // 设置物流评价
        if (po.getDeliveryEvaluation() != null) {
            vo.setDeliveryEvaluation(po2VO(po.getDeliveryEvaluation()));
            // 给订单评价设置门店属性
            vo.setDepotCode(po.getDeliveryEvaluation().getDepotCode());
        }
        // 设置商品评价
        if (po.getProductEvaluations() != null && !po.getProductEvaluations().isEmpty()) {
            vo.setProductEvaluations(
                    // 转化商品评价
                    po.getProductEvaluations()
                            .stream()
                            .map(productEvaluation -> po2VO(productEvaluation))
                            .collect(Collectors.toList()));
            // 再次给订单评价设置门店属性
            if (StringUtils.isBlank(vo.getDepotCode())) {
                // 取第一条商品评价
                ProductEvaluation productEvaluation = po.getProductEvaluations().get(0);
                vo.setDepotCode(productEvaluation == null ? null : productEvaluation.getDepotCode());
            }
        }
        return vo;
    }

    /**
     * 物流评价PO转VO
     * @param po
     * @return
     */
    public static DeliveryEvaluationVO po2VO(DeliveryEvaluation po) {
        if (po == null) {
            return null;
        }
        DeliveryEvaluationVO vo = new DeliveryEvaluationVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    /**
     * 商品评价PO转VO
     * @param po
     * @return
     */
    public static ProductEvaluationVO po2VO(ProductEvaluation po) {
        if (po == null) {
            return null;
        }
        ProductEvaluationVO vo = new ProductEvaluationVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

}
